package dosgi.core.tracker;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

import dosgi.core.hook.HooksManager;
import dosgi.distribution.remote.RemoteInvocationClient;

@SuppressWarnings({ "rawtypes" , "unchecked" })
public class RemoteInvokerTrackerCustomizer implements ServiceTrackerCustomizer  {

	private BundleContext context;
	private HooksManager hooksManager;
	
	public RemoteInvokerTrackerCustomizer(HooksManager hooksManager, BundleContext context) {
		this.context = context;
		this.hooksManager = hooksManager;
	}
	
	@Override
	public Object addingService(ServiceReference serviceReference) {
		RemoteInvocationClient client = context.getService(serviceReference);
		
		hooksManager.setQueueName(client.getQueueName());
		
		return null;
	}

	@Override
	public void modifiedService(ServiceReference arg0, Object arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removedService(ServiceReference serviceReference, Object arg1) {
		hooksManager.setQueueName(null);

		
		context.ungetService(serviceReference);
	}

}
